package org.syyo.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.syyo.admin.common.constant.PageConstant;
import org.syyo.admin.common.enums.ResultEnum;
import org.syyo.admin.common.exception.SysException;
import org.syyo.admin.domain.ResultVo;
import org.syyo.admin.domain.request.MenuReq;
import org.syyo.admin.domain.response.MenuResp;
import org.syyo.admin.mapper.MenuMapper;
import org.syyo.admin.service.MenuService;
import org.syyo.admin.utils.*;

import java.util.Date;
import java.util.List;

/**
 * @Auther: wangzhong
 * @Date: 2019/10/8 17:36
 * @Description:
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private PageConstant pageConstant;

    /**
     * 添加菜单
     * @param req
     * @return
     */
    @Override
    @Transactional
    public ResultVo addMenu(MenuReq req) {
        CheckUtils.validate(req);
        //将int列表转换成字符串 [1,2,3] => "1,2,3"
        String pids = MyStringUtils.listToString(req.getPidList());
        req.setPids(pids);
        //添加创建时间
        Date date = DateUtils.getDate();
        Integer pid = req.getPid();
        // pid 为空时，默认设置为0,0表示顶级菜单
        if (pid == null){
            pid = 0;
        }
        Integer type = req.getType();
        if (type == null){
            req.setType(1);
        }
        req.setPid(pid);
        req.setCreateDate(date);
        int i = menuMapper.addMenu(req);
        if (i != 1){
            throw new SysException(ResultEnum.MENU_ADD_FAIL.getCode(),ResultEnum.MENU_ADD_FAIL.getMessage());
        }
        return ResultUtils.response(i);
    }

    /**
     * 删除菜单
     * @param req
     * @return
     */
    @Override
    @Transactional
    public ResultVo delMenu(MenuReq req) {
        //校验菜单id
        Integer menuId = req.getMenuId();
        MenuResp menuResp = menuMapper.getMenu(menuId);
        if (menuResp == null){
            throw new SysException(ResultEnum.MENU_NOT_EXIST.getCode(),ResultEnum.MENU_NOT_EXIST.getMessage());
        }
        //删除菜单
        int i = menuMapper.delMenu(menuId);
        if (i != 1){
            throw new SysException(ResultEnum.MENU_DEL_FAIL.getCode(),ResultEnum.MENU_DEL_FAIL.getMessage());
        }
        return ResultUtils.response(i);
    }

    /**
     * 修改菜单
     * @param req
     * @return
     */
    @Override
    @Transactional
    public ResultVo editMenu(MenuReq req) {
        Integer menuId = req.getMenuId();
        //校验菜单id
        MenuResp menuResp = menuMapper.getMenu(menuId);
        if (menuResp == null){
            throw new SysException(ResultEnum.MENU_NOT_EXIST.getCode(),ResultEnum.MENU_NOT_EXIST.getMessage());
        }

        Integer pid = req.getPid();
        if (menuId.equals(pid)){
            throw new SysException(ResultEnum.MENU_PID_NOT_SELF.getCode(),ResultEnum.MENU_PID_NOT_SELF.getMessage());
        }

        //将int列表转换成字符串 [1,2,3] => "1,2,3"
        String pids = MyStringUtils.listToString(req.getPidList());
        req.setPids(pids);
        //添加更新时间
        Date date = DateUtils.getDate();
        req.setUpdateDate(date);
        int i = menuMapper.editMenu(req);
        if (i != 1){
            throw new SysException(ResultEnum.MENU_UPDATE_FAIL.getCode(),ResultEnum.MENU_UPDATE_FAIL.getMessage());
        }
        return ResultUtils.response(i);
    }

    /**
     * 菜单详情
     * @param menuId
     * @return
     */
    @Override
    public ResultVo getMenu(Integer menuId) {
        MenuResp menu = menuMapper.getMenu(menuId);
        //pids 转成pidList "0,2,4" => [0,2,4]
        String pids = menu.getPids();
        List<Integer> list = MyStringUtils.stringToList(pids);
        menu.setPidList(list);
        return ResultUtils.response(menu);
    }

    /**
     * 菜单列表
     * @param req
     * @return
     */
    @Override
    public ResultVo listMenu(MenuReq req) {
        List<MenuResp> menuResps = menuMapper.listMenu(req);
        //pids 转成  "0,2,4" => [0,2,4]
        menuResps = stringToList(menuResps);

        Integer pageNum = req.getPageNum();
        Integer pageSize = req.getPageSize();
        if(pageNum == null ){
            pageNum = pageConstant.getPageNum();
        }
        if(pageSize == null || pageSize > pageConstant.getPageRange() ){
            pageSize = pageConstant.getPageSize();
        }
        //分页处理
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<MenuResp> list = new PageInfo<>(menuResps);

        return ResultUtils.response(list);
    }

    /**
     * 菜单列表树形结构
     * @param req
     * @return
     */
    @Override
    public ResultVo treeMenu(MenuReq req) {
        List<MenuResp> menuResps = menuMapper.listMenu(req);
        //pids 转成pidList "0,2,4" => [0,2,4]
        menuResps = stringToList(menuResps);
        List<MenuResp> menuResps1 = TreeUtils.listToMenuTree(menuResps);
        return ResultUtils.response(menuResps1);
    }

    /**
     *   将字符串装成列表 "0,2,4" => [0,2,4]
     * @param menuResps
     * @return
     */
    private List<MenuResp> stringToList(List<MenuResp> menuResps) {
        if (!MyListUtils.isEmpty(menuResps)){
            for (MenuResp menuResp : menuResps) {
                String pids = menuResp.getPids();
                List<Integer> list = MyStringUtils.stringToList(pids);
                menuResp.setPidList(list);
            }
        }
        return menuResps;
    }

}
